package com.roc.web.action.setting;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblClearTxn;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.Utils;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/setting")
public class ClearAction  extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5236170890634487270L;
	private Logger logger = LogManager.getLogger(ClearAction.class);

	private ClearSearchCondition searchCondition;

	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired 
	IMerchantServices merchantServices;
	
	@Autowired
	private IPowerInfoService powerInfoService;

	private String msg;
	// 清算初始页面。
	
	protected Map<String,String> tranMap = ConfigProperties.getSysDB1("TRANS_CD",false);

	@Action(value = "clearlist", results = {
			@Result(name = "success", location = "/pages/setting/clearlist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp"),
			@Result(name = "error", location = "/error.jsp") }

	)
	public String clearlist() throws Exception {
		initSomeDataForAction();
		searchCondition = new ClearSearchCondition();
		getRequest().setAttribute("tranMap",tranMap);
		return SUCCESS;
	}
	

	// 清算查询
	@Action(value = "clearsearch", results = {
			@Result(name = "success", location = "/pages/setting/clearlist.jsp"),
			@Result(name = "error", location = "/pages/setting/clearlist.jsp"),
			@Result(name = "login", location = "/pages/redirect.jsp") })
	public String search() {
	
		if (searchCondition == null) {
			searchCondition = (ClearSearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new ClearSearchCondition();
			}
		}
		try {
			// 系统管理员查看全部，代理只能看到本机构下的。
//			if (Constance.MERCHANT_TYPE_DAILI.equals(logingMerchant
//					.getRoleType())) {
//				searchCondition.setOrgCode(logingMerchant.getOrgCode());
//			}
/*			List<Object> objs = settflagService.querycalcuSummoney(searchCondition);*/

			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);

			Map<String, String> map=powerInfoService.queryRealIdByOpId(op.getSuperiorId(), op.getRoleType(), op.getId(), op.getOrgCode());
	           // String orgCode=map.get("orgCode");
	         String superRole= map.get("superRole");
	         String opRealId =map.get("opRealId");
			// 查询 当前代理商及子代理商 数据
			searchCondition.setRoleType(superRole);
			if(Role.MERCHANT.getCode().equals(superRole)){
				searchCondition.setMerId(op.getAccountNo());
			}else if(Role.AGENT.getCode().equals(superRole)||Role.SFB_AGENT.getCode().equals(superRole)){
				
				String agentId= opRealId;
				searchCondition.setAgentId(agentId);
				
			}else if(Role.AGENT_BUSS.getCode().equals(superRole)){
				searchCondition.setOpId(opRealId);
			}else if(Role.MARKET_DIRECT.getCode().equals(superRole) || Role.MARKET_LEAD.getCode().equals(superRole)){
				searchCondition.setMarketorId(opRealId);
			}else if(Role.O_MANAGER.getCode().equals(superRole)){//
				searchCondition.setUcbccdArea(op.getOneagentid());
			}/*else if(Role.OPERATORS_COMM.getCode().equals(op.getRoleType()) && !Constant.SD_ORG_CD.equals(op.getOneagentid())){//运维人员
				searchCondition.setUcbccdArea(op.getOneagentid());
			}*/
			 searchCondition.setOrgCode(op.getOrgCode());
			
			int rows = pagenitionService.countSQL(searchCondition);
			List<TblClearTxn> clearList = new ArrayList<TblClearTxn>();
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				if (searchRsList != null && searchRsList.size() > 0) {
					Iterator iterator = searchRsList.iterator();
					while (iterator.hasNext()) {

						Object[] obj = (Object[]) iterator.next();
						TblClearTxn clear = new TblClearTxn();
					
						if (obj[0] != null && !"".equals(obj[0])) {
							clear.setMchtCd(obj[0].toString());
						}
						if (obj[1] != null && !"".equals(obj[1])) {
							clear.setMchtName(obj[1].toString());
						}
						if (obj[2] != null && !"".equals(obj[2])) {
							clear.setTermId(obj[2].toString());
						}
						if (obj[3] != null && !"".equals(obj[3])) {/*
							clear.setPan(obj[3].toString());*/
							// 主账号隐藏中间数字
							clear.setPan(Utils.dealCardNo(Constance.Null2EmptyTrim(obj[3].toString())));
						}
						if (obj[4] != null && !"".equals(obj[4])) {
							clear.setStlmDate(obj[4].toString());
						}
						if (obj[5] != null && !"".equals(obj[5])) {
							clear.setTransDateTime(Utils.getTime(obj[5].toString()));
						}
						if (obj[6] != null && !"".equals(obj[6])) {
							clear.setTransAmt(obj[6].toString());
						}
						if (obj[7] != null && !"".equals(obj[7])) {
							clear.setMchtFee(obj[7].toString());
						}
						if (obj[8] != null && !"".equals(obj[8])) {
							clear.setMchtSetAmt(obj[8].toString());
						}
						if (obj[9] != null && !"".equals(obj[9])) {
							clear.setMaSettleDtStr(obj[9].toString());
						}
						if (obj[10] != null && !"".equals(obj[10])) {
							clear.setTxnDesc(tranMap.get(obj[10].toString()));
						}
						if (obj[11] != null && !"".equals(obj[11])) {
							clear.setSysSer(obj[11].toString());
						}
						if (obj[12] != null && !"".equals(obj[12])) {
							clear.setCardKindDis(obj[12].toString());
						}
						clearList.add(clear);
					}
				}
			}
			this.setTotalPage(this.calcuTotalPage(rows));
/*			double totalAmt=0;
			double totalFee=0;
			double totalCalciAmt=0;
			int count=0;*/
/*			if(objs!=null&&objs.size()>0){
				Iterator iterator = objs.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					 totalAmt=Double.valueOf(String.valueOf(obj[0]));
					 totalFee=Double.valueOf(String.valueOf(obj[1]));
					 totalCalciAmt=Double.valueOf(String.valueOf(obj[2]));
					 count=Integer.valueOf(String.valueOf(obj[3]));
				}
			}
			addActionMessage("当前条件下清算总金额【"+totalAmt+"】元，总手续费【"+totalFee+"】，总结算金额【"+totalCalciAmt+"】，总清算笔数【"+count+"】");*/
			addActionMessage("当前条件下统计到【"+rows+"】条记录");
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute("clearList", clearList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			getRequest().setAttribute("tranMap",tranMap);
			setSearchToConditionSession(searchCondition);

		} catch (ServiceException e) {

			logger.error(e.getMessage(), e);

		} catch (Exception e) {
			logger.error(e.getMessage(), e);

		}
		return SUCCESS;
	}
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		return searchCondition;
	}
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_CALCU, searchCondition);
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (ClearSearchCondition) getSession().getAttribute(
				SEARCH_SESSION_BEAN_CALCU);
	}
	@Override
	protected String getInitPageSize() {
		return null;
	}


	public ClearSearchCondition getSearchCondition() {
		return searchCondition;
	}


	public void setSearchCondition(ClearSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}


	public String getMsg() {
		return msg;
	}


	public void setMsg(String msg) {
		this.msg = msg;
	}

	
	
	
}
